package com.videomeetting.legend.videomeetting.activity.service;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.display.DisplayManager;
import android.os.IBinder;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

import com.videomeetting.legend.videomeetting.activity.ControlActivity;
import com.videomeetting.legend.videomeetting.activity.base.BaseApplication;

import org.video.stream.Mmixer;

public class HdmiSecService extends Service{
	BaseApplication application;
	private ControlActivity.CHInfo[] chAll = null;
	private Mmixer mMixer = null;
	private DisplayManager mDisplayManager;
	private static HdmiSecPresentation myPresentation = null;

	@SuppressLint("NewApi")
	private void showPresentation(Display display) {
		if (myPresentation == null) {
			myPresentation = new HdmiSecPresentation(this, display, mMixer);
			myPresentation.setOnDismissListener(new DialogInterface.OnDismissListener() {
				@Override
				public void onDismiss(DialogInterface dialog) {
//					Log.d("corelib", "=======XgguoReceiver====onDismiss===================");
				}
			});
			myPresentation.getWindow().setType(WindowManager.LayoutParams.TYPE_PHONE);
			myPresentation.show();
		}
	}
	@SuppressLint("NewApi")
	public void updateContents(int hdmi) {
		mDisplayManager = (DisplayManager) getSystemService(
				Context.DISPLAY_SERVICE);
		Display[] displays = mDisplayManager.getDisplays();
//		Log.d("corelib", ">>>> display count " + displays.length);
		showPresentation(displays[hdmi]);
	}
	private void registerMountEvent() {
		IntentFilter localIntentFilter = new IntentFilter();
		localIntentFilter.addAction("android.set");

		registerReceiver(this.XgguoReceiver, localIntentFilter);
	}
	private void nnRegisterMountEvent()
	{
		unregisterReceiver(this.XgguoReceiver);
	}
	private BroadcastReceiver XgguoReceiver = new BroadcastReceiver(){
		@SuppressLint("NewApi")
		public void onReceive(Context context, Intent intent){
			System.out.println("xxx");
			if(intent.getAction().equals("android.set")){
				if (intent.getIntExtra("paramInt",0) == 1) {
					updateContents(1);
				}
				else{
					if(myPresentation != null){
						myPresentation.onDismiss();
						myPresentation.releaseMediaPlayer();
						myPresentation.dismiss();
						myPresentation.cancel();
						myPresentation = null;
					}
				}
			}
			registerMountEvent();
		}
	};

	public IBinder onBind(Intent paramIntent){
		return null;
	}
	public void onCreate(){
		super.onCreate();

		application = (BaseApplication) getApplication();
		mMixer = application.getmMixer();
		chAll = application.getInfo();
		registerMountEvent();
	}
	public void onDestroy(){
//		Log.d("corelib", "=======XgguoReceiver====onDestroy===================");
		super.onDestroy();
		nnRegisterMountEvent();

		if(myPresentation != null){
			myPresentation.onDismiss();
			myPresentation.releaseMediaPlayer();
			myPresentation.dismiss();
			myPresentation.cancel();
			myPresentation = null;
		}
	}
}
